package com.teddy.DTXMB.Controller;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.teddy.DTXMB.Model.Product;
import com.teddy.DTXMB.Service.ProductService;
import com.teddy.DTXMB.Model.Custom;
import com.teddy.DTXMB.Service.CustomService;

@Controller
@SessionAttributes("custom")
public class CustomController {
	
	private static final Logger logger = LoggerFactory.getLogger(CustomController.class);
	/*加入@AutoWired，即可以自动初始化这个类的实例，不需要再在下面的方法中初始化了，和这个类的@Service配合使用*/
	@Autowired
	private ProductService productService;
	@Autowired
	private CustomService customService;
	
	/*登陆，成功则返回到“我的页面”*/
	@RequestMapping(value = "/signin", method = RequestMethod.POST)
	public String signIn(@RequestParam("name") String name, @RequestParam("pwd") String pwd, Model model) {
		Custom custom = customService.getCustom(name, pwd);
		if(custom != null){
			/*若登陆成功，返回 个人资料 以及 热卖产品*/
			model.addAttribute("custom", custom);
			Product hotProduct = productService.getHotProduct();
			model.addAttribute("hotproduct", hotProduct);
			logger.info("Custom Logged Success: " + custom.getName());
			return "mypage";
		}
		else{
			/*登陆失败，返回到“错误用户”页面*/
			logger.info("Custom Logged Failed...");
			return "errorcustom";
		}
	}
	
	/*防止直接输入/signin来进行访问而得到405 error，暂时没去找合理的写法*/
	@RequestMapping(value = "/signin", method = RequestMethod.GET)
	public String signIn2(HttpServletRequest request, Model model){
		/*若在session中检查出正确的用户登陆信息，则跳到“我的主页”，否则跳到“错误用户”页面*/
		if(request.getSession().getAttribute("custom") != null){
			return "redirect:/mypage";
		}
		else{
			logger.info("Custom Logged Failed...");
			return "errorcustom";
		}
	}
	
	/*“我的主页”*/
	@RequestMapping(value = "/mypage", method = RequestMethod.GET)
	public String signIn(HttpServletRequest request, Model model) {
		if(request.getSession().getAttribute("custom") != null){
			/*用户信息，热卖产品*/
			Custom custom = (Custom) request.getSession().getAttribute("custom");
			model.addAttribute("custom", custom);
			Product hotProduct = productService.getHotProduct();
			model.addAttribute("hotproduct", hotProduct);
			logger.info("Custom Logged Success...");
			return "mypage";
		}
		else{
			logger.info("Custom Logged Failed...");
			return "errorcustom";
		}
	}
	
	/*登出，返回到主页*/
	@RequestMapping(value = "/signout", method = RequestMethod.POST)
	public String signOut(Model model) {
		/*新实例化custom，既是登出*/
		model.addAttribute("custom", new Custom());
		return "redirect:/"; 
	}
	
	/*防止直接输入/signout来进行访问而得到405 error，暂时没去找合理的写法*/
	@RequestMapping(value = "/signout", method = RequestMethod.GET)
	public String signOut2(HttpServletRequest request, Model model){
		/*若在session中检查出正确的用户登陆信息，则跳到“我的主页”，否则跳到“错误用户”页面*/
		if(request.getSession().getAttribute("custom") != null){
			return "redirect:/mypage";
		}
		else{
			logger.info("Custom Logged Failed...");
			return "errorcustom";
		}
	}
}
